$(function(){

    var bodywidth=$(window).width();
    var thiswidth=$('.comment-content').width();
    var offwidth=(bodywidth-thiswidth)/2<0?0:(bodywidth-thiswidth)/2;
    $(".comment-content").css({top:"200px",left:offwidth+'px'})



	var morewidth=$(".total-lead").offset().left;
	var widthlead=$(".total-lead").width();
	$("[class=none]").width(morewidth);
	$(".sq-search").width(morewidth*2+widthlead);

	var allid=[];
	var allpart=[];
	var $li=$(".parts>li");
	$li.eq(0).addClass("addbc").children("#add-icon").removeClass("hidden");

    var $model=$("#model");
    $("#model").remove();
    $model.removeClass("hidden").removeAttr("id");
	$.ajax({
		url:'?r=ajax/community',
		dataType:'json',
		method:'post',
		data:{
			state:'2'
		},
		success:function(data){
			$(".table tr:gt(0)").remove();
			var dat=data;
			var length=data.length;
			allid=[];
			allpart=[];
			for(var i=0; i<length; i++){
				allid.push(data[i].question_id);
				allpart.push(data[i].class1_name);
				var $clone=$model.clone(true);
				$clone=clone($clone,data[i]);
				$clone.insertAfter(".table tr:last-child");
				if (i>9) {
					$clone.hide();
				}
			}
			$(".addmore>span").click(function(){
				var n=0;
				$(".table tr:gt(0)").each(function(){
					if (!$(this).is(':visible')) {
						n++;
						$(this).slideDown(200);
						if (n>5) {
							$(this).hide();
						}
					}
				})
			})
			//点击图标播放
            $(".nowrecord img").click(function(){
            	var $audio=$(this).siblings("audio");
                if ($audio[0].paused) {    //已停止
                    $audio[0].play();
                    $(this).attr("src","./images/xx.gif");
                   
                    // 浏览量以及播放量暂时未写
                    // var tarid=data[0].user_id;
                    // $.ajax({
                    //     url:'?r=ajax/view',
                    //     method:'post',
                    //     dataType:'json',
                    //     data:{
                    //         'tar':tarid,
                    //         'sou':current_userid,
                    //         'class2':que_info.class2,
                    //         'class1':que_info.class1,
                    //         'question':que_info.question,
                    //     }
                    // })
                }else{                  //正在播放
                    $audio[0].currentTime=0;
                    $audio[0].pause();
                    $(this).attr("src","./images/xx.jpg");
                }
            });
            setInterval(function(){
                if ($(".nowrecord audio")[0].ended){
                	$(".nowrecord img").attr("src","./images/xx.jpg");
                }
            },100)



			//点击表格跳处弹框
			var $tr=$(".table tr");
			var $tr=$(".table tr");
			$tr.click(function(){
				var index=$tr.index(this)-1;
				var $this=$(this);
				var time=$this.find('.date').text();
				var writer=$this.find('.thiswriter').text();
				var goods=$this.find('.goodtimes').text();
				var views=$this.find('.playtimes').text();
				var $nowcomment=$(".comment-content");
				if (data[index].class1_name=='writing') {
				    var comments=data[index].comment_content;			//选择音频或者是评论内容
					$nowcomment.find('.user-id').text(writer);
					$nowcomment.find('.user-date').text(time);
					$nowcomment.find('.goods').text(goods);
					$nowcomment.find('.views').text(views);
					$nowcomment.find('.user-comments').html(comments).removeClass("hidden");
					$nowcomment.find('.nowrecord').addClass("hidden");
					$nowcomment.find('#linkit a').attr("href",getHref(allpart[index],allid[index]));		//          -------------》地址
				}else{
					var src=$this.find('audio').attr("src");
				    var comments=data[index].comment_content;			//选择音频或者是评论内容
					$nowcomment.find('.user-id').text(writer);
					$nowcomment.find('.user-date').text(time);
					$nowcomment.find('.goods').text(goods);
					$nowcomment.find('.views').text(views);
					$nowcomment.find('.user-comments').addClass("hidden");
					$nowcomment.find('.nowrecord').removeClass("hidden").find("audio").attr("src",src);
					$nowcomment.find('#linkit a').attr("href",getHref(allpart[index],allid[index]));
				}
				$nowcomment.slideDown(300);
			})
			$("#close").click(function(){
				$(".comment-content").slideUp(300);
			})
		}
	});


	$li.click(function(){
		var $this=$(this);
		$li.each(function(){
			$(this).removeClass("addbc").children("#add-icon").addClass("hidden");
		});
		$this.addClass("addbc").children("#add-icon").removeClass("hidden");
		var index=$li.index(this);
		var data=select(index);
		$.ajax({
			url:'?r=ajax/community',
			dataType:'json',
			data:data,
			method:'post',
			success:function(data){
				$(".table tr:gt(0)").remove();
				var length=data.length;
				allid=[];
				allpart=[];
				for(var i=0; i<length; i++){
					allid.push(data[i].question_id);
					allpart.push(data[i].class1_name);
					var $clone=$model.clone(true);
					$clone=clone($clone,data[i]);
					$clone.insertAfter(".table tr:last-child");
					if (i>9) {
						$clone.hide();
					}
				}
				$(".addmore>span").click(function(){
					var n=0;
					$(".table tr:gt(0)").each(function(){
						if (!$(this).is(':visible')) {
							n++;
							$(this).slideDown(200);
							if (n>5) {
								$(this).hide();
							}
						}
					})
				})


				var $tr=$(".table tr");
				$tr.click(function(){
					var index=$tr.index(this)-1;
					var $this=$(this);
					var time=$this.find('.date').text();
					var writer=$this.find('.thiswriter').text();
					var goods=$this.find('.goodtimes').text();
					var views=$this.find('.playtimes').text();
					var $nowcomment=$(".comment-content");
					if (data[index].class1_name=='writing') {
					    var comments=data[index].comment_content;			//选择音频或者是评论内容
						$nowcomment.find('.user-id').text(writer);
						$nowcomment.find('.user-date').text(time);
						$nowcomment.find('.goods').text(goods);
						$nowcomment.find('.views').text(views);
						$nowcomment.find('.user-comments').html(comments).removeClass("hidden");
						$nowcomment.find('.nowrecord').addClass("hidden");
						$nowcomment.find('#linkit a').attr("href",getHref(allpart[index],allid[index]));
					}else{
						var src=$this.find('audio').attr("src");
					    var comments=data[index].comment_content;			//选择音频或者是评论内容
						$nowcomment.find('.user-id').text(writer);
						$nowcomment.find('.user-date').text(time);
						$nowcomment.find('.goods').text(goods);
						$nowcomment.find('.views').text(views);
						$nowcomment.find('.user-comments').addClass("hidden");
						$nowcomment.find('.nowrecord').removeClass("hidden").find("audio").attr("src",src);
						$nowcomment.find('#linkit a').attr("href",getHref(allpart[index],allid[index]));
					}
					$nowcomment.slideDown(300);
				})
				$("#close").click(function(){
					$(".comment-content").slideUp(300);
				})


				//点击图标播放
	            $(".nowrecord img").click(function(){
	            	var $audio=$(this).siblings("audio");
	                if ($audio[0].paused) {    //已停止
	                    $audio[0].play();
	                    $(this).attr("src","./images/xx.gif");
	                    

	                    // 播放量先未完成
	                    // var tarid=data[0].user_id;
	                    // $.ajax({
	                    //     url:'?r=ajax/view',
	                    //     method:'post',
	                    //     dataType:'json',
	                    //     data:{
	                    //         'tar':tarid,
	                    //         'sou':current_userid,
	                    //         'class2':que_info.class2,
	                    //         'class1':que_info.class1,
	                    //         'question':que_info.question,
	                    //     }
	                    // })
	                }else{                  //正在播放
	                    $audio[0].currentTime=0;
	                    $audio[0].pause();
	                    $(this).attr("src","./images/xx.jpg");
	                }
            	});
	            setInterval(function(){
	                if ($(".nowrecord audio")[0].ended){
	                	$(".nowrecord img").attr("src","./images/xx.jpg");
	                }
	            },500)
			}
		})
	})





});
function setHot(data,$demo){
    $demo.find(".play-record audio").attr("src",data.url);
    if (data.photo) {
		$demo.find(".user-pic img").attr("src",data.photo);
	}else{
		$demo.find(".user-pic img").attr("src","<?=Url::to('@web/images/default.jpg');?>");
	}
    $demo.find(".user-id").text(data.name);
    $demo.find(".user-date").text(data.comment_time);
    $demo.find(".views").text(data.play_num);
    $demo.find(".goods").text(data.like_num);
    $demo.find(".user-comments").html(changeText(changeData(data.comment_content)));
    var likepeo=data.like_ids;
    if (likepeo.indexOf(current_userid)!=-1&&likepeo.length>0) {
        $demo.find(".good-times>span").addClass("isgood");
    }
}
function clone($demo,data){
	if (data.class1_name=='writing') {
		$demo.find(".kinds").text(fun(data.class1_name));
		$demo.find(".part").text(getTnum(data.class2_name,data.tnum));
		$demo.find(".main").text();
        $demo.find(".wordover").removeClass("hidden").html(changeData(data.comment_content));
        var tx=$demo.find(".wordover").text();
        $demo.find(".wordover").text(tx);
		$demo.find(".thiswriter").text(data.user_name);
		$demo.find(".playtimes").text(data.play_num);
		$demo.find(".goodtimes").text(data.like_num);
		$demo.find(".date").text(data.comment_time);
	}else{
		$demo.find(".kinds").text(fun(data.class1_name));
		$demo.find(".part").text(getTnum(data.class2_name,data.tnum));
		$demo.find(".main").text();
		$demo.find('audio').attr("src",data.url);
		$demo.find(".recordimg").removeClass("hidden").find('img').attr("src","./images/xx.jpg");
		$demo.find(".thiswriter").text(data.user_name);
		$demo.find(".playtimes").text(data.play_num);
		$demo.find(".goodtimes").text(data.like_num);
		$demo.find(".date").text(data.comment_time);
	}
	return $demo;
}
function fun(val){
	switch(val+''){
		case 'speaking':return '口语';break;
		case 'writing':return '写作';break;
	}
}
function changeData(val){
    val=val.replace(/\s{2,}|\<br\s*\/\>|\<br\>/g,' ');      //超过两个空字符或者是<br></br>都替换成空格
    return val;
}
function select(val){
	var data={};
	switch(val+''){
		case '0':{
			data.state=2;
			return data;
		}break;
		case '1':{
			data.class='speaking';
			data.state=1;
			return data;
		}break;
		case '2':{
			data.class='writing';
			data.state=1;
			return data;
		}break;
	}
}
function getHref(class1,id){
	return '?r=community/'+class1+'&id='+id;
}
function getTnum(val,tnum){
	if(tnum=='0'){
		return val.toUpperCase();
	}else{
		return val.toUpperCase()+' - '+tnum;
	}
}